(function () {
    // if(!auth()) return

    $.ajax({
        url: 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=getSignPackage',
        data: {
            url: window.location.href
        },
        dataType: 'json',
        type: 'post',
        success: function (res) {
            if (res.retcode === 0) {
                wx.config({
                    debug: false,
                    appId: res.data.appId,
                    timestamp: res.data.timestamp,
                    nonceStr: res.data.nonceStr,
                    signature: res.data.signature,
                    jsApiList: ['config', 'chooseImage', 'uploadImage', 'getLocalImgData']
                })
            }
        }
    })

    wx.ready(function () {
        $(".upload-input").on("click", function (e) {
            e.preventDefault();
            wx.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'],
                sourceType: ['album', 'camera'],
                success: function (resp) {
                    $('.upload-image').attr('src', resp.localIds[0])
                    $('.loading-mask').addClass('show')
                    wx.getLocalImgData({
                        localId: resp.localIds[0],
                        success: function (res) {
                            var localData = res.localData;
                            // localData = localData.substr(localData.indexOf('base64,') + 7)
                            $('.loading-mask').addClass('show')
                            $.ajax({
                                url: 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=uploadBase64',
                                data: { img: localData },
                                dataType: 'json',
                                type: 'post',
                                success: function (res) {
                                    $('.loading-mask').removeClass('show')
                                    if (res.retcode === 0) {
                                        $('#after .name').val(res.cardInfo.name)
                                        $('#after .idcard').val(res.cardInfo.id)
                                        $('#after .nation').val(res.cardInfo.nation)
                                        $('#after .gender').val(res.cardInfo.gender)
                                        $('#after .address').val(res.cardInfo.address)
                                        checkInput('.photo-tab')
                                        $(".upload").addClass('uploaded')
                                    } else if (res.retcode === 302) {
                                        window.location.href = res.redirect_uri
                                    } else {
                                        // alert(res.retmsg)
                                        $("#ocrfail-alert").addClass('show')
                                    }
                                }
                            })
                        }
                    });
                }
            });
        })
    });

    $("#confirm-btn").click(function() {
        $("#ocrfail-alert").removeClass('show')
    })

    checkInput('.input-tab')
    $(".input-tab input").on("change", function (e) {
        checkInput('.input-tab')
    })
    $(".tab-element").on("click", function (e) {
        let attr = $(e.currentTarget).attr('data-tab')
        $(".tab-block").removeClass("input photo")
        $(".tab-block").addClass(attr)
    })

    var nations = [{ "value": "01", "text": "汉族" }, { "value": "02", "text": "蒙古族" }, { "value": "03", "text": "回族" }, { "value": "04", "text": "藏族" }, { "value": "05", "text": "维吾尔族" }, { "value": "06", "text": "苗族" }, { "value": "07", "text": "彝族" }, { "value": "08", "text": "壮族" }, { "value": "09", "text": "布依族" }, { "value": "10", "text": "朝鲜族" }, { "value": "11", "text": "满族" }, { "value": "12", "text": "侗族" }, { "value": "13", "text": "瑶族" }, { "value": "14", "text": "白族" }, { "value": "15", "text": "土家族" }, { "value": "16", "text": "哈尼族" }, { "value": "17", "text": "哈萨克族" }, { "value": "18", "text": "傣族" }, { "value": "19", "text": "黎族" }, { "value": "20", "text": "傈僳族" }, { "value": "21", "text": "佤族" }, { "value": "22", "text": "畲族" }, { "value": "23", "text": "高山族" }, { "value": "24", "text": "拉祜族" }, { "value": "25", "text": "水族" }, { "value": "26", "text": "东乡族" }, { "value": "27", "text": "纳西族" }, { "value": "28", "text": "景颇族" }, { "value": "29", "text": "柯尔克孜族" }, { "value": "30", "text": "土族" }, { "value": "31", "text": "达斡尔族" }, { "value": "32", "text": "仫佬族" }, { "value": "33", "text": "羌族" }, { "value": "34", "text": "布朗族" }, { "value": "35", "text": "撒拉族" }, { "value": "36", "text": "毛难族" }, { "value": "37", "text": "仡佬族" }, { "value": "38", "text": "锡伯族" }, { "value": "39", "text": "阿昌族" }, { "value": "40", "text": "普米族" }, { "value": "41", "text": "塔吉克族" }, { "value": "42", "text": "怒族" }, { "value": "43", "text": "乌孜别克族" }, { "value": "44", "text": "俄罗斯族" }, { "value": "45", "text": "鄂温克族" }, { "value": "46", "text": "崩龙族" }, { "value": "47", "text": "保安族" }, { "value": "48", "text": "裕固族" }, { "value": "49", "text": "京族" }, { "value": "50", "text": "塔塔尔族" }, { "value": "51", "text": "独龙族" }, { "value": "52", "text": "鄂伦春族" }, { "value": "53", "text": "赫哲族" }, { "value": "54", "text": "门巴族" }, { "value": "55", "text": "珞巴族" }, { "value": "56", "text": "基诺族" }]
    var picker = new Picker({
        data: [nations],
        selectedIndex: [0]
    });
    picker.on('picker.select', function (selectedVal, selectedIndex) {
        $("#nation").val(nations[selectedIndex[0]].text);
        $("#nation").trigger("change")
    })
    $(".select-nation").on("click", function (e) {
        picker.show();
    })

    $(".next-btn").on("click", function (e) {
        var tab = $(e.target).closest('.switch-tab')
        var info = checkInput('.' + tab[0].classList[1])
        if (info.status) {
            $.ajax({
                url: 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=saveCardInfo',
                data: info.data,
                dataType: 'json',
                type: 'post',
                success: function (res) {
                    if (res.retcode === 0) {
                        window.location.href = "regist_steptwo.html"
                    } else if (res.retcode === 302) {
                        window.location.href = res.redirect_uri
                    } else  {
                        alert(res.retmsg)
                    }
                }
            })
        }
    })

    $(".quick-bind").on("click", function (e) {
        window.location.href = "https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=authhealthCode"
    })

   /*$(".upload-input").on("change", function (e) {
        var blob = e.target.files[0]
        var reader = new FileReader();
        reader.onload = (e) => {
            $('.upload-image').attr('src', e.target.result)
            $('.loading-mask').addClass('show')
            var form = new FormData(document.getElementById("img-form"))
            $.ajax({
                url: 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=upload',
                data: form,
                contentType: false,
                processData: false,
                dataType: 'json',
                type: 'post',
                success: function (res) {
                    $('.loading-mask').removeClass('show')
                    if (res.retcode === 0) {
                        $('#after .name').val(res.cardInfo.name)
                        $('#after .idcard').val(res.cardInfo.id)
                        $('#after .nation').val(res.cardInfo.nation)
                        $('#after .gender').val(res.cardInfo.gender)
                        $('#after .address').val(res.cardInfo.address)
                        checkInput('.photo-tab')
                        $(".upload").addClass('uploaded')
                    } else if (res.retcode === 302) {
                        window.location.href = res.redirect_uri
                    } else {
                        alert(res.retmsg)
                    }
                }
            })
        }
        reader.readAsDataURL(blob)
    })*/

})()

function checkInput(fa) {
    var name = $(fa+ " .name").val()
    var idCard = $(fa+" .idcard").val()
    var nation = $(fa+" .nation").val()
    if (name && idCard && nation) {
        $(fa+" .next-btn").removeAttr('disabled')
        return { status: true, data: {name, idCard, nation }}
    } else {
        $(fa+" .next-btn").attr('disabled', "disabled")
        return { status: false, data: {name, idCard, nation }}
    }
}

function auth() {
    var login = getCookie('isLogin') || ''
    var AuthCode = getCookie('isAuthCode') || ''
    if (login === '') {
        window.location.href = 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=login'
        return false
    } else if (AuthCode === '') {
        window.location.href = 'https://healthcarddemo.tengmed.com/index.php?c=healthcard&a=authwechatCode'
        return false
    } else {
        return true
    }
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) {
                c_end = document.cookie.length
            }
            return unescape(document.cookie.substring(c_start, c_end))
        }
    }
    return ""
}
